import { MutationTree } from 'vuex';
import { State } from './state';
import { MutationType } from '@/enums/userEnums';
import { storage } from '@/utils';

export type Mutations<S = State> = {
    [MutationType.SetToken](state: S, payload: string): void;
    [MutationType.SetAvatar](state: S, payload: string): void;
    [MutationType.SetInfo](state: S, payload: { [x: string]: any }): void;
    [MutationType.SetName](state: S, payload: string): void;
    [MutationType.SetNickName](state: S, payload: string): void;
    [MutationType.SetPassword](state: S, payload: string): void;
    [MutationType.SetRemember](state: S, payload: boolean): void;
    [MutationType.SetRole](state: S, payload: string[]): void;
};

export const mutations: MutationTree<State> & Mutations = {
    [MutationType.SetToken]: (state, token) => {
        state.token = token;
        storage.set(MutationType.SetToken, token, 1000 * 60 * 60 * 12);
    },
    [MutationType.SetAvatar]: (state, avatar) => {
        state.avatar = avatar;
    },
    [MutationType.SetName]: (state, username) => {
        state.username = username;
        storage.set(MutationType.SetName, username);
    },
    [MutationType.SetNickName]: (state, nickName) => {
        state.nickName = nickName;
        storage.set(MutationType.SetName, nickName);
    },
    [MutationType.SetPassword]: (state, password) => {
        state.password = password;
        storage.set(MutationType.SetName, password);
    },
    [MutationType.SetInfo]: (state, userInfo) => {
        state.userInfo = userInfo;
        storage.set(MutationType.SetInfo, userInfo);
    },
    [MutationType.SetRemember]: (state, isRemember) => {
        state.isRemember = isRemember;
        storage.set(MutationType.SetRemember, isRemember);
    },
    [MutationType.SetRole]: (state, roles) => {
        state.roles = roles;
        storage.set(MutationType.SetRole, roles);
    },
};
